﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
namespace EzFrameWork
{
    public class EzAudioComponent : MonoBehaviour
    {
        Dictionary<string, AudioClip> audioDic = new Dictionary<string, AudioClip>();
        Dictionary<string, AudioSource> BGM_Dic = new Dictionary<string, AudioSource>();
        Dictionary<string, AudioClip> AudioClip_Dic = new Dictionary<string, AudioClip>();
        Dictionary<string, AudioSource> Audio_Dic = new Dictionary<string, AudioSource>();
        GameObject AUDIO_OBJ;



        /// <summary>
        /// 播放背景音乐
        /// </summary>
        public void PlayBGM(string path, bool isLoop = true)
        {
            if (!BGM_Dic.ContainsKey(path))
            {
                GameObject go = new GameObject("BGM_Obj" + BGM_Dic.Count.ToString());
                go.transform.parent = transform;
                go.AddComponent<AudioSource>();
                AudioSource audioSource = go.GetComponent<AudioSource>();
                StartCoroutine(LoadBGM(audioSource, path));
                Debug.Log("PlayAudio");
            }
            else
            {
                BGM_Dic[path].Play();
            }
        }

        /// <summary>
        /// 停止所有BGM
        /// </summary>
        public void StopAllBgm(string path)
        {
            foreach (var item in BGM_Dic)
            {
                item.Value.Stop();
            }
        }

        /// <summary>
        /// 停止所有BGM
        /// </summary>
        public void StopBgm(string path)
        {
            if (BGM_Dic.ContainsKey(path))
            {
                BGM_Dic[path].Stop();
            }
            else
            {
                Debug.LogError("不存在路径:" + path + "1.请检查之前是否播放过此路径的音频，如果之前没有播放过此音频是无法暂停的。2.检查一下路径是否正确");
            }
        }

        /// <summary>
        /// 根据路径加载音频
        /// </summary>
        /// <param name="path"></param>
        /// <param name="audioType"></param>
        public void PlayAudio(string type, string path)
        {
            AudioSource audioSource;
            if (!AudioClip_Dic.ContainsKey(path)) //如果没有这个类型的声音组
            {
                GameObject go = new GameObject(type);
                go.transform.parent = transform;
                go.AddComponent<AudioSource>();
                audioSource = go.GetComponent<AudioSource>();
                StartCoroutine(LoadAudio(audioSource, type, path));
            }
            else
            {
                if (Audio_Dic.ContainsKey(path))
                {
                    Audio_Dic[path].Play();
                }
                else
                {
                    Debug.LogError(path + "错误！！");
                }
            }
        }
        public void PlayOneShotAudio(string type, string path)
        {
            AudioSource audioSource;
            if (!AudioClip_Dic.ContainsKey(path)) //如果没有这个类型的声音组
            {
                GameObject go = new GameObject(type);
                go.transform.parent = transform;
                go.AddComponent<AudioSource>();
                audioSource = go.GetComponent<AudioSource>();
                StartCoroutine(LoadAudio(audioSource, type, path));
            }
            else
            {
                if (Audio_Dic.ContainsKey(path))
                {
                    Audio_Dic[path].PlayOneShot(Audio_Dic[path].clip);
                }
                else
                {
                    Debug.LogError(path + "错误！！");
                }
            }
        }
        //创建一个AudioSource物体
        public void CreateAudioSourceItem(string type, string path, Vector3 position,UnityAction<AudioSource> action)
        {
            GameObject audioGameObject = new GameObject(type);
            AudioSource audioSource = audioGameObject.AddComponent<AudioSource>();
            EzMonoManager.Instance.StartCoroutine(LoadAudio(audioSource, type, path, clip =>
            {
                audioSource.clip = clip;
                action(audioSource);
            }));
        }
        //创建一个AudioSource物体
        public void CreateAudioSourceItem(string type, string path, Transform parent, UnityAction<AudioSource> action)
        {
            GameObject audioGameObject = new GameObject(type);
            audioGameObject.transform.SetParent(parent);
            AudioSource audioSource = audioGameObject.AddComponent<AudioSource>();
            EzMonoManager.Instance.StartCoroutine(LoadAudio(audioSource, type, path, clip =>
            {
                audioSource.clip = clip;
                action(audioSource);
            }));
        }
        public void CreateAudioSourceItem(string type, string path, UnityAction<AudioSource> action)
        {
            GameObject audioGameObject = new GameObject(type);
            AudioSource audioSource = audioGameObject.AddComponent<AudioSource>();
            EzMonoManager.Instance.StartCoroutine(LoadAudio(audioSource, type, path, clip =>
             {
                 audioSource.clip = clip;
                 action(audioSource);
             }));
        }
        private IEnumerator LoadAudio(AudioSource audioSource, string type, string recordPath)
        {
            WWW www = new WWW(recordPath);
            Debug.Log("播放路径为" + recordPath);
            yield return www;
            AudioClip temp = www.GetAudioClip();
            AudioClip_Dic.Add(recordPath, temp);
            Audio_Dic.Add(recordPath, audioSource);
            Audio_Dic[recordPath].clip = AudioClip_Dic[recordPath];
            Audio_Dic[recordPath].Play();
        }
        private IEnumerator LoadAudio(AudioSource audioSource, string type, string recordPath, UnityAction<AudioClip> action)
        {
            WWW www = new WWW(recordPath);
            Debug.Log("播放路径为" + recordPath);
            yield return www;
            AudioClip temp = www.GetAudioClip();
            AudioClip_Dic.Add(recordPath, temp);
            Audio_Dic.Add(recordPath, audioSource);
            action(temp);
        }

        private IEnumerator LoadBGM(AudioSource audioSource, string recordPath, bool isloop = true)
        {
            WWW www = new WWW(recordPath);
            Debug.Log("播放路径为" + recordPath);
            yield return www;
            AudioClip audioClip = www.GetAudioClip();
            audioDic.Add(recordPath, audioClip);
            BGM_Dic.Add(recordPath, audioSource);
            BGM_Dic[recordPath].clip = audioDic[recordPath];
            BGM_Dic[recordPath].loop = isloop;
            BGM_Dic[recordPath].Play();
        }
        
        // Use this for initialization
        void Start()
        {
            DontDestroyOnLoad(gameObject);
        }

    }
}